package services

import (
	"ContentSystem/internal/api/operate"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type ContentFindReq struct {
	Id       int    `json:"id" `
	Page     int    `json:"page" binding:"required" `      // 页
	Author   string `json:"author" `                       // 页
	Title    string `json:"title" `                        // 页
	PageSize int    `json:"page_size" binding:"required" ` //页大小
}

type Content struct {
	Title          string        `json:"title" binding:"required" `
	Description    string        `json:"description" binding:"required" `
	Author         string        `json:"author" binding:"required"`
	VideoURL       string        `json:"video_url"`
	Thumbnail      string        `json:"thumbnail"`
	Category       string        `json:"category"`
	Duration       time.Duration `json:"duration"`
	Resolution     string        `json:"resolution"`
	FileSize       string        `json:"file_size"`
	Format         string        `json:"format"`
	Quality        int           `json:"quality"`
	ApprovalStatus int           `json:"approval_status"`
	CreatedAt      time.Time     `json:"created_at"`
	UpdatedAt      time.Time     `json:"updated_at"`
}
type ContentFindResp struct {
	Message  string    `json:"message"`
	Contents []Content `json:"contents"`
	Total    int64     `json:"total"`
}

func (c *CmsApp) ContentFind(ctx *gin.Context) {

	var req ContentFindReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	resp, err := c.operateAppClient.FindContent(ctx, &operate.FindContentReq{
		Id:       int64(req.Id),
		Page:     string(req.Page),
		PageSize: string(req.PageSize),
		Author:   req.Author,
		Title:    req.Title,
	})
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	// 调用本服务的方法
	//contentDao := dao.NewContentDao(c.db)

	//contentList, total, err := contentDao.Find(&dao.FindParams{
	//	Id:       *req.Id,
	//	Page:     req.Page,
	//	PageSize: req.PageSize,
	//	Author:   req.Author,
	//	Title:    req.Title,
	//})
	//contents := make([]Content, 0, len(contentList))
	//for _, content := range contentList {
	//	// 组装数据
	//	contents = append(contents, Content{
	//		Title:          content.Title,
	//		Description:    content.Description,
	//		Author:         content.Author,
	//		VideoURL:       content.VideoURL,
	//		Thumbnail:      "",
	//		Category:       "",
	//		Duration:       0,
	//		Resolution:     "",
	//		FileSize:       "",
	//		Format:         "",
	//		Quality:        0,
	//		ApprovalStatus: 0,
	//		CreatedAt:      time.Time{},
	//		UpdatedAt:      time.Time{},
	//	})
	//}

	// 响应
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "ok",
		"data":    resp,
	})
}
